package org.example.work.Mapper;

import org.apache.ibatis.annotations.*;
import org.example.work.entity.buser;

import java.util.List;

@Mapper
public interface Busermapper {
    List<buser> selectAll();
    @Insert({"insert into user(name,password)values(#{name},#{password})"})
    int insert(String name,String password);
    @Select({"select *from user where id=#{id}"})
    buser selectById(int id);
    @Select({"select *from user where name=#{name} and password=#{password}"})
    buser selectByName(String name,String password);
//    @Select("SELECT u.id AS user_id, u.name, u.password, o.ordid, o.totprice " +
//            "FROM user u JOIN orders o ON u.id = o.user_id")
//    @Results({
//            @Result(property = "id", column = "user_id"),
//            @Result(property = "name", column = "name"),
//            @Result(property = "password", column = "password"),
//            @Result(property = "orders", column = "user_id",
//                    javaType = List.class,
//                    many = @Many(select = "org.example.work.Mapper.Ordersmapper.selectbyid"))
//    })
//    List<buser> selectwithOrder();
//@Select("select id,ordid,totprice,name,password from user as u join orders as o on u.id=o.user_id")
//@Results({
//        @Result(property = "id", column = "id"),
//        @Result(property = "name", column = "name"),
//        @Result(property = "password", column = "password"),
//        @Result(property = "orders", column = "id",
//                javaType = List.class,
//                many = @Many(select = "org.example.work.Mapper.Ordersmapper.selectbyid"))
//})
//List<buser> selectwithOrder();
//    第一行 @Result(property = "id", column = "id") 是将 SQL 查询结果中的 id 字段映射到 User 实体类的 id 属性。这里没有别名，字段名和属性名一致，所以直接映射。
//    第四行是关键，涉及多表关联。property = "orders" 表示映射到 User 实体类的 orders 属性，column = "id" 表示从 SQL 查询结果中提取 id 字段的值，作为参数传递给 @Many 查询方法。javaType = List.class 指定 orders 是一个 List 类型，many = @Many 定义了关联查询的逻辑
@Select("SELECT *FROM user u")
@Results({
        @Result(property = "id", column = "id"),
        @Result(property = "name", column = "name"),
        @Result(property = "password", column = "password"),
        @Result(property = "orders", column = "id",
                javaType = List.class,
                many = @Many(select = "org.example.work.Mapper.Ordersmapper.selectbyid"))
})
List<buser> selectwithOrder();
    @Update({"update user set password=#{password} where name=#{name}"})
    int updatePassword(String name,String password);

}
